﻿using System;
using System.Collections;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using ThePresenter.Windows.Data;

namespace ThePresenter.Windows.Controls
{
	/// <summary>
	/// Interaction logic for ShapeBar.xaml
	/// </summary>
	public partial class ShapeBar : ListBox
	{
		#region Constants

		/// <summary>
		/// Minimal drag distance.
		/// </summary>
		const double MIN_DRAG_DIST = 15; 

		#endregion

		#region Fields

		private String _ShapesLibraryPath;

		private DragDropInfo _DragDropInfo;

		#endregion

		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="ShapeBar"/> class.
		/// </summary>
		public ShapeBar()
		{
			InitializeComponent();

			_ShapesLibraryPath = ThePresenter.Properties.Settings.Default.ShapesLibraryPath;
		}

		#endregion

		#region Methods

		/// <summary>
		/// Starts the dragging.
		/// </summary>
		private void StartDragging()
		{
			DataObject data = new DataObject(_DragDropInfo.DraggedShape.Strokes);

			if (data != null)
			{
				_DragDropInfo.IsDragging = true;
				DragDrop.DoDragDrop(this, data, DragDropEffects.Copy);
				data = null;
			}

			// Reset all data involved in D&D
			_DragDropInfo.IsDragging = false;
			_DragDropInfo.DraggedShape = null;
		}

		/// <summary>
		/// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.PreviewMouseDown"/> attached routed event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
		/// </summary>
		/// <param name="e">The <see cref="T:System.Windows.Input.MouseButtonEventArgs"/> that contains the event data. The event data reports that one or more mouse buttons were pressed.</param>
		protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed && (e.OriginalSource as FrameworkElement).DataContext is Shape)
			{
				_DragDropInfo.DragStartPoint = e.GetPosition(null);
				_DragDropInfo.DraggedShape = (e.OriginalSource as FrameworkElement).DataContext as Shape;
				_DragDropInfo.IsDragging = false;
			}
			else
			{
				_DragDropInfo.DraggedShape = null;
			}

			base.OnPreviewMouseDown(e);
		}

		/// <summary>
		/// Invoked when an unhandled <see cref="E:System.Windows.Input.Mouse.PreviewMouseMove"/> attached event reaches an element in its route that is derived from this class. Implement this method to add class handling for this event.
		/// </summary>
		/// <param name="e">The <see cref="T:System.Windows.Input.MouseEventArgs"/> that contains the event data.</param>
		protected override void OnPreviewMouseMove(MouseEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed // mouse is acctualy down
				&& _DragDropInfo.DraggedShape != null // we are dragging shape
				&& !_DragDropInfo.IsDragging) // we are not already doing DragDrop
			{
				Point cur_point = e.GetPosition(null);
				double dist = Math.Sqrt(Math.Pow(_DragDropInfo.DragStartPoint.X - cur_point.X, 2) + Math.Pow(_DragDropInfo.DragStartPoint.Y - cur_point.Y, 2));

				if (dist >= MIN_DRAG_DIST)
				{
					StartDragging();
				}
			}

			base.OnPreviewMouseMove(e);
		}

		#endregion

		#region Event handlers

		/// <summary>
		/// Handles the Click event of the Insert control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void Insert_Click(object sender, RoutedEventArgs e)
		{
			//if (sender is MenuItem)
			if (SelectedItem is Shape)
			{
				//Shape shape = (((sender as MenuItem).Parent as ContextMenu).PlacementTarget as Control).DataContext as Shape;
				Shape shape = SelectedItem as Shape;
			}
		}

		/// <summary>
		/// Handles the Click event of the Rename control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void Rename_Click(object sender, RoutedEventArgs e)
		{
			if (SelectedItem is Shape)
			{
				var panel = (((((sender as MenuItem).Parent as ContextMenu).PlacementTarget as ContentControl).Content as Image).Parent as ContentControl).Parent as Panel;

				(panel.Children[1] as FrameworkElement).Focus();
			}
		}

		/// <summary>
		/// Handles the Click event of the Delete control.
		/// </summary>
		/// <param name="sender">The source of the event.</param>
		/// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
		private void Delete_Click(object sender, RoutedEventArgs e)
		{
			var shape = SelectedItem as Shape;

			if (shape != null)
			{
				if (MessageBox.Show(string.Format(ThePresenter.Properties.Resources.DeleteShapeQuestion, shape.Name), ThePresenter.Properties.Resources.Question, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
				{
					(ItemsSource as IList).Remove(SelectedItem);
				}
			}

			e.Handled = true;
		} 

		#endregion
	}
}
